<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Handshaking (Clients)</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Boost.Beast">
<link rel="up" href="../using_websocket.html" title="Using WebSocket">
<link rel="prev" href="establishing_connections.html" title="Establishing Connections">
<link rel="next" href="handshaking_servers.html" title="Handshaking (Servers)">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="establishing_connections.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="handshaking_servers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.using_websocket.handshaking_clients"></a><a class="link" href="handshaking_clients.html" title="Handshaking (Clients)">Handshaking
      (Clients)</a>
</h3></div></div></div>
<p>
        A WebSocket session begins when a client sends the HTTP/1 <a href="https://tools.ietf.org/html/rfc7230#section-6.7" target="_top">Upgrade</a>
        request for <a href="https://tools.ietf.org/html/rfc6455#section-1.3" target="_top">websocket</a>,
        and the server sends an appropriate response indicating that the request
        was accepted and that the connection has been upgraded. The Upgrade request
        must include the <a href="https://tools.ietf.org/html/rfc7230#section-5.4" target="_top">Host</a>
        field, and the <a href="https://tools.ietf.org/html/rfc7230#section-5.3" target="_top">target</a>
        of the resource to request. The stream member functions <a class="link" href="../ref/boost__beast__websocket__stream/handshake/overload1.html" title="websocket::stream::handshake (1 of 4 overloads)"><code class="computeroutput"><span class="identifier">handshake</span></code></a> and <a class="link" href="../ref/boost__beast__websocket__stream/async_handshake/overload1.html" title="websocket::stream::async_handshake (1 of 2 overloads)"><code class="computeroutput"><span class="identifier">async_handshake</span></code></a> are used to send
        the request with the required host and target strings.
      </p>
<pre class="programlisting"><span class="identifier">ws</span><span class="special">.</span><span class="identifier">handshake</span><span class="special">(</span><span class="string">"localhost"</span><span class="special">,</span> <span class="string">"/"</span><span class="special">);</span>
</pre>
<p>
        The implementation will create and send a request that typically looks like
        this:
      </p>
<div class="table">
<a name="beast.using_websocket.handshaking_clients.websocket_upgrade_http_request"></a><p class="title"><b>Table&#160;1.24.&#160;WebSocket Upgrade HTTP Request</b></p>
<div class="table-contents"><table class="table" summary="WebSocket Upgrade HTTP Request">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Serialized Octets
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody><tr>
<td>
<pre class="programlisting"><span class="identifier">GET</span> <span class="special">/</span> <span class="identifier">HTTP</span><span class="special">/</span><span class="number">1.1</span>
<span class="identifier">Host</span><span class="special">:</span> <span class="identifier">localhost</span>
<span class="identifier">Upgrade</span><span class="special">:</span> <span class="identifier">websocket</span>
<span class="identifier">Connection</span><span class="special">:</span> <span class="identifier">upgrade</span>
<span class="identifier">Sec</span><span class="special">-</span><span class="identifier">WebSocket</span><span class="special">-</span><span class="identifier">Key</span><span class="special">:</span> <span class="number">2</span><span class="identifier">pGeTR0DsE4dfZs2pH</span><span class="special">+</span><span class="number">8</span><span class="identifier">MA</span><span class="special">==</span>
<span class="identifier">Sec</span><span class="special">-</span><span class="identifier">WebSocket</span><span class="special">-</span><span class="identifier">Version</span><span class="special">:</span> <span class="number">13</span>
<span class="identifier">User</span><span class="special">-</span><span class="identifier">Agent</span><span class="special">:</span> <span class="identifier">Beast</span>
</pre>
              </td>
<td>
                <p>
                  The host and target parameters become part of the Host field and
                  request-target in the resulting HTTP request. The key is generated
                  by the implementation. Callers may add fields or modify fields
                  by providing a <span class="emphasis"><em>decorator</em></span>, described below.
                </p>
              </td>
</tr></tbody>
</table></div>
</div>
<br class="table-break"><h5>
<a name="beast.using_websocket.handshaking_clients.h0"></a>
        <span class="phrase"><a name="beast.using_websocket.handshaking_clients.decorators"></a></span><a class="link" href="handshaking_clients.html#beast.using_websocket.handshaking_clients.decorators">Decorators</a>
      </h5>
<p>
        If the caller wishes to add or modify fields, the member functions <a class="link" href="../ref/boost__beast__websocket__stream/handshake_ex.html" title="websocket::stream::handshake_ex"><code class="computeroutput"><span class="identifier">handshake_ex</span></code></a> and <a class="link" href="../ref/boost__beast__websocket__stream/async_handshake_ex.html" title="websocket::stream::async_handshake_ex"><code class="computeroutput"><span class="identifier">async_handshake_ex</span></code></a> are provided
        which allow an additional function object, called a <span class="emphasis"><em>decorator</em></span>,
        to be passed. The decorator is invoked to modify the HTTP Upgrade request
        as needed. This example sets a subprotocol on the request:
      </p>
<pre class="programlisting"><span class="identifier">ws</span><span class="special">.</span><span class="identifier">handshake_ex</span><span class="special">(</span><span class="string">"localhost"</span><span class="special">,</span> <span class="string">"/"</span><span class="special">,</span>
    <span class="special">[](</span><span class="identifier">request_type</span><span class="special">&amp;</span> <span class="identifier">m</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">m</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">sec_websocket_protocol</span><span class="special">,</span> <span class="string">"xmpp;ws-chat"</span><span class="special">);</span>
    <span class="special">});</span>
</pre>
<p>
        The HTTP Upgrade request produced by the previous call will look thusly:
      </p>
<div class="table">
<a name="beast.using_websocket.handshaking_clients.decorated_websocket_upgrade_http"></a><p class="title"><b>Table&#160;1.25.&#160;Decorated WebSocket Upgrade HTTP Request</b></p>
<div class="table-contents"><table class="table" summary="Decorated WebSocket Upgrade HTTP Request">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Serialized Octets
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody><tr>
<td>
<pre class="programlisting"><span class="identifier">GET</span> <span class="special">/</span> <span class="identifier">HTTP</span><span class="special">/</span><span class="number">1.1</span>
<span class="identifier">Host</span><span class="special">:</span> <span class="identifier">localhost</span>
<span class="identifier">Upgrade</span><span class="special">:</span> <span class="identifier">websocket</span>
<span class="identifier">Connection</span><span class="special">:</span> <span class="identifier">upgrade</span>
<span class="identifier">Sec</span><span class="special">-</span><span class="identifier">WebSocket</span><span class="special">-</span><span class="identifier">Key</span><span class="special">:</span> <span class="number">2</span><span class="identifier">pGeTR0DsE4dfZs2pH</span><span class="special">+</span><span class="number">8</span><span class="identifier">MA</span><span class="special">==</span>
<span class="identifier">Sec</span><span class="special">-</span><span class="identifier">WebSocket</span><span class="special">-</span><span class="identifier">Version</span><span class="special">:</span> <span class="number">13</span>
<span class="identifier">Sec</span><span class="special">-</span><span class="identifier">WebSocket</span><span class="special">-</span><span class="identifier">Protocol</span><span class="special">:</span> <span class="identifier">xmpp</span><span class="special">;</span><span class="identifier">ws</span><span class="special">-</span><span class="identifier">chat</span>
<span class="identifier">User</span><span class="special">-</span><span class="identifier">Agent</span><span class="special">:</span> <span class="identifier">Beast</span>
</pre>
              </td>
<td>
                <p>
                  Undefined behavior results if the decorator modifies the fields
                  specific to perform the WebSocket Upgrade , such as the Upgrade
                  and Connection fields.
                </p>
              </td>
</tr></tbody>
</table></div>
</div>
<br class="table-break"><h5>
<a name="beast.using_websocket.handshaking_clients.h1"></a>
        <span class="phrase"><a name="beast.using_websocket.handshaking_clients.filtering"></a></span><a class="link" href="handshaking_clients.html#beast.using_websocket.handshaking_clients.filtering">Filtering</a>
      </h5>
<p>
        When a client receives an HTTP Upgrade response from the server indicating
        a successful upgrade, the caller may wish to perform additional validation
        on the received HTTP response message. For example, to check that the response
        to a basic authentication challenge is valid. To achieve this, overloads
        of the handshake member function allow the caller to store the received HTTP
        message in an output reference argument of type <a class="link" href="../ref/boost__beast__websocket__response_type.html" title="websocket::response_type"><code class="computeroutput"><span class="identifier">response_type</span></code></a> as follows:
      </p>
<pre class="programlisting"><span class="identifier">response_type</span> <span class="identifier">res</span><span class="special">;</span>
<span class="identifier">ws</span><span class="special">.</span><span class="identifier">handshake</span><span class="special">(</span><span class="identifier">res</span><span class="special">,</span> <span class="string">"localhost"</span><span class="special">,</span> <span class="string">"/"</span><span class="special">);</span>
<span class="keyword">if</span><span class="special">(!</span> <span class="identifier">res</span><span class="special">.</span><span class="identifier">count</span><span class="special">(</span><span class="identifier">http</span><span class="special">::</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">sec_websocket_protocol</span><span class="special">))</span>
    <span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">invalid_argument</span><span class="special">(</span><span class="string">"missing subprotocols"</span><span class="special">);</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2016, 2017 Vinnie Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="establishing_connections.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="handshaking_servers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
